package com.example.smartcity_poverty.ui.AllService.LoveDonate.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.LoveDonate.LoveActivityAllModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_show_list.*
import kotlinx.android.synthetic.main.re_lovedinate_show_list.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 公益列表页面展示
 */
class ShowActivityListActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_show_list)
        LoveTb.title = "公益列表"
        LoveTb.setNavigationOnClickListener { finish() }

        intent.getStringExtra("id")?.toInt().apply {
            // 填充数据
            setIdShowList(this)
        }
    }
    // Alert
    private fun alert(id: Int) {
        // 获取布局
        val view = View.inflate(this, R.layout.activity_lovedonate_juanzhu, null)
        // 获取控件
        val juanzhu = view.findViewById<EditText>(R.id.juanzhuEd)
        AlertDialog.Builder(this).apply {
            setTitle("捐助")
            setView(view)
            setPositiveButton("捐助") { _, _ ->
                if (getToken().isNotEmpty()) {
                    CoroutineScope(Dispatchers.Main).launch {
                        api.getJuanZhu(getToken(), JSONObject().apply {
                            put("activityId", id)
                            put("donateMoney", juanzhu.text.toString())
                        }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                            if (this.code == "200"){
                                runOnUiThread { this.msg.show() }
                            }else runOnUiThread { this.msg.show() }
                        }
                    }
                } else "请先登录！".show()
            }
            setNegativeButton("取消") { _, _ ->

            }
            show()
        }
    }

    private fun setIdShowList(id: Int?) {
        if (getToken().isNotEmpty()) {
            CoroutineScope(Dispatchers.Main).launch {
                api.getLoveActivityAll(getToken(), id!!).apply {
                    if (this.code == 200) {
                        this.rows?.let {
                            ShowListRv.apply {
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter =
                                    ShowListAdapter(ArrayList<LoveActivityAllModel.RowsBean>().apply {
                                        this.addAll(it)
                                    }, R.layout.re_lovedinate_show_list)
                                this.isNestedScrollingEnabled = false
                            }
                        }
                    } else runOnUiThread { "加载列表出错？".show() }
                }
            }
        } else "加载列表出错！".show()
    }


    /**
     * 列表适配器
     */
    inner class ShowListAdapter(
        val list: MutableList<LoveActivityAllModel.RowsBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<LoveActivityAllModel.RowsBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: LoveActivityAllModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val fv = holder.viewFind
            val ct = fv.view?.context

            glide(url + d.imgUrl, fv.ActShowImage)
            fv.ActShowTitle.text = d.name
            fv.ActShowName.text = "发布人: ${d.author}"
            fv.ActShowTime.text = "项目时间: ${d.createTime}"
            fv.ActShowUserAll.text = "已筹善款: ${d.moneyNow}"
            fv.ActShowUserSize.text = "参捐人数: ${d.donateCount}"
            fv.ActShowContent.text = "公益内容: ${d.type?.name}"

            // 发起捐助
            fv.ActShowBtn.setOnClickListener {
                alert(d.id)
            }
        }

    }
}